import http from '@/utils/request'
import { PROFILE_PROFILE, PROFILE_USER } from '../action_types'

// 存储用户自己的信息
export const seveuser = (payload) => {
  return {
    type: PROFILE_USER,
    payload,
  }
}

// 用户自己信息的请求
export const getuser = () => {
  return async (dispatch) => {
    const res = await http.get('/user')
    dispatch(seveuser(res.data))
  }
}

// 存储用户个人资料
export const setUserProfile = (profile) => ({
  type: PROFILE_PROFILE,
  payload: profile,
})

// 获取用户个人资料
export const getUserProfile = () => {
  return async (dispatch) => {
    const res = await http.get('/user/profile')
    dispatch(setUserProfile(res.data))
  }
}

// 修改用户个人资料
export const updateProfile = (data) => {
  return async (dispatch) => {
    await http.patch('/user/profile', data)
    dispatch(getUserProfile(data))
  }
}

// 修改用户头像
export const updatephoto = (fd) => {
  return async (dispatch) => {
    await http.patch('/user/photo', fd)
    dispatch(getUserProfile(fd))
  }
}
